// pages/pay/cardList/cardList.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    cardList: {
      type: Object,
      value: [],
    },
    cardData: {
      type: Object,
      value: {},
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    selCardId: '',
    selData: {},
    selCard: false,
    imgHttp: app.globalData.imgHttp, //图片前缀
    propShow: false,
  },
  attached() {
    this.getDatail()

  },
  /**
   * 组件的方法列表
   */
  methods: {

    getDatail() {
      console.log('cardData', this.properties.cardData)
      console.log(this.data.cardData.id)
      if (this.properties.cardData.id != undefined) {
        this.data.selCard = true
        console.log(1111)
      }
      this.setData({
        selData: this.properties.cardData,
        selCardId: this.properties.cardData.id,
        selCard: this.data.selCard,
      })
      console.log(this.data.selCard)
    },
    //关闭弹窗
    handleChos() {
      this.triggerEvent('handleChose')
      console.log(this.data.selCard)
    },
    handleConfirm() {
      console.log(this.data.selCard)
      console.log(this.data.selData.qrCode)
      if (this.data.selData.qrCode != '' && this.data.selData.qrCode!=undefined) {
        this.setData({
          propShow: true
        })
      } else {
        this.setData({
          propShow: false
        })
        this.triggerEvent('handleChose')
        this.triggerEvent('handleConfirm', this.data.selData)
        this.triggerEvent('handleselCard', this.data.selCard)
      }
    },
    closepropShow(){ //关闭核销二维码弹窗
      this.setData({
        propShow: false
      })
    },
    //选择卡
    handleBiao(e) {
      console.log(this.data.selCard)
      let that = this
      var id = e.currentTarget.dataset.id
      var item = e.currentTarget.dataset.item
      console.log('item', item)
      var selCardId = this.data.selCardId
      if (item.balanceMoney == 0 && !this.data.selCard && item.cardType!=7) {
        wx.showModal({
          title: '卡内余额：0元',
          content: '卡内余额不足，是否先去充值',
          cancelText: '继续选择',
          confirmText: '去充值',
          cancelColor: '#666666',
          confirmColor: '#FF600B',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/userPageA/pages/topup/topup?customerCardId=' + item.id,
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
              if (selCardId == id) {
                that.setData({
                  selCardId: '',
                  selData: {},
                  selCard: false,
                })
              } else {
                that.setData({
                  selCardId: id,
                  selData: item,
                  selCard: true,
                })
              }
            }
          }
        })
        return
      }

      if (selCardId == id) {
        this.setData({
          selCardId: '',
          selData: {},
          selCard: false,
        })
      } else {
        this.setData({
          selCardId: id,
          selData: item,
          selCard: true,
        })
      }
    },
  }
})